package com.apestech.framework.dictionary.lib;

import java.sql.Connection;

import com.apestech.formwork.stream.ArrayObjectStreamHander;
import com.apestech.formwork.stream.ListStream;
import com.apestech.framework.db.SqlSession;
import com.apestech.framework.db.callback.QueryCallBackHandler;
import com.apestech.framework.dictionary.DataTableCache;
import com.apestech.framework.dictionary.modul.Table;
import com.apestech.framework.metadata.MetaData;
import com.apestech.framework.stream.ArrayObject;
import com.apestech.framework.util.FileUtil;
import com.apestech.framework.util.StringUtil;
import com.apestech.framework.db.SqlResultSet;
import com.apestech.framework.file.FileDictionary.LogSaveType;

 /*
   1）初始化，直接查询全部字段信息，根据字段信息做表名的数据汇总
   2）单表信息分热部署，和集中式计算两种模式
 */

public class WriteDictionary extends StringUtil {
	
	private String fileName;
	private SqlSession session;
	
	public WriteDictionary(SqlSession session, String fileName) throws Exception{
		this.fileName=fileName;
		this.session=session;
	}
	
	private ListStream getWriter(String file, String keyField) throws Exception{
		String f=this.fileName+"\\"+file;
		f=FileUtil.getFileNameForSys(f);
		ListStream ls=new ListStream(f, 100, LogSaveType.KEY);
		ls.setKeyLength(-1);
		ls.setKeyField(keyField);
		return ls;
	}
	
	public void getTable() throws Exception{
		FileUtil.deleteDir(FileUtil.getFileNameForSys(this.fileName+"\\table"));
		writeTable(null);
	}
	
	public void getTable(String tabName) throws Exception{
		FileUtil.deleteDir(this.fileName+"\\table\\"+tabName);
		writeTable(tabName);
	}

	public Table writeTableToDB() throws Exception{
		String tabName=fileName;
		Connection conn=null;
		conn=session.getConnection();
		MetaData md=new MetaData(conn, session.getSchema());
		DataTableCache datatable=new DataTableCache(session, md, fileName);
		if(tabName==null){
			md.getColumns(datatable.getQueryCallBackHandler());
		} else {
			md.getColumn(tabName, datatable.getQueryCallBackHandler());
		}
		return datatable.getTab();
	}

	private void writeTable(String tabName) throws Exception{
		SqlResultSet rs=null;
		Connection conn=null;
		boolean flag=true;
		try{
			conn=session.getConnection();
			if(conn==null){
				conn=session.getDataSource().getConnection();
				flag=false;
			}
			ListStream ls=getWriter("table" ,"TABLE_NAME");
			MetaData md=new MetaData(conn, session.getSchema());
			QueryCallBackHandler qcb=ls.write(md);
			if(tabName==null){
				md.getColumns(qcb);
			} else {
				md.getColumn(tabName, qcb);
			}
		} finally {
			if(conn!=null && !flag) {
				conn.close();
			}
		}
	}
	
	public void getProcedure() throws Exception{
		SqlResultSet rs=null;
		Connection conn=null;
		try{
			conn=session.getDataSource().getConnection();
			MetaData md=new MetaData(conn, session.getSchema());
			FileUtil.deleteDir(this.fileName+"\\proc");
			rs=md.getProcedure();
			ListStream ls=getWriter("proc","PROCEDURE_NAME");
			ls.write(new ArrayObject(rs), new ArrayObjectStreamHander(rs, new ProcRowHander(rs)));
		} finally {
			if(rs!=null) rs.close();
			if(conn!=null) {
				conn.close();
			}
		}
	}
	
	public void getProcedure(String procName) throws Exception{
		SqlResultSet rs=null;
		Connection conn=null;
		try{
			conn=session.getDataSource().getConnection();
			MetaData md=new MetaData(conn, session.getSchema());
			FileUtil.deleteDir(this.fileName+"\\proc\\"+procName);
			rs=md.getProcedure(procName);
			ListStream ls=getWriter("proc", "PROCEDURE_NAME");
			ls.write(new ArrayObject(rs), new ArrayObjectStreamHander(rs, new ProcRowHander(rs)));
		} finally {
			if(rs!=null) rs.close();
			if(conn!=null) {
				conn.close();
			}
		}
	}

}
